package com.lynch.mp3;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lynch.model.Mp3Info;
import com.lynch.mp3.service.PlayerService;

public class PlayerActivity extends Activity {
	
	ImageButton beginButton = null;
	ImageButton pauseButton = null;
	ImageButton stopButton = null;
	MediaPlayer mediaPlayer = null;
	TextView lrcText = null;
	private Mp3Info mp3Info = null;
	
	private IntentFilter intentFilter;
	private BroadcastReceiver receiver;
	
	@Override
	protected void onPause() {
		super.onPause();
		System.out.println("取消歌词更新");
		unregisterReceiver(receiver);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		System.out.println("从新进行歌词更新");
		receiver=new LrcMessageBroadcastReceiver();
		registerReceiver(receiver, getIntentFilter());
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player);
		Intent itent = getIntent();
		mp3Info =(Mp3Info)itent.getSerializableExtra("mp3Info");
		
		beginButton = (ImageButton)findViewById(R.id.begin);
		pauseButton = (ImageButton)findViewById(R.id.pause);
		stopButton = (ImageButton)findViewById(R.id.stop);
		lrcText = (TextView) findViewById(R.id.lrcText);
		
		OnClickListener buttonListener = new ButtonListener();
		beginButton.setOnClickListener(buttonListener);
		pauseButton.setOnClickListener(buttonListener);
		stopButton.setOnClickListener(buttonListener);
	}
	
	class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			
			Intent intent=new Intent();
			intent.setClass(PlayerActivity.this, PlayerService.class);
			ImageButton button=(ImageButton)v;
			switch (button.getId()) {
			case R.id.begin:
				intent.putExtra("mp3Info", mp3Info);
				intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG);
				startService(intent);
				break;
				
			case R.id.pause:
				intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG);
				startService(intent); 
				break;
				
			case R.id.stop:
				intent.putExtra("MSG", AppConstant.PlayerMsg.STOP_MSG);
				startService(intent);
				break;
				
			default:
				break;
			}
		}
	}
	
	/**生成intentFilter*/
	private IntentFilter getIntentFilter() {
		System.out.println("生成intent过滤器");
		if (intentFilter==null) {
			intentFilter=new IntentFilter();
			intentFilter.addAction(AppConstant.LRC_MESSAGE_ACTION);
		}
		return intentFilter;
	}
	
	/**广播接收器，主要是接受service发送的广播，并且更新ui，就是放置歌词的textview*/
	class LrcMessageBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			System.out.println("进入广播接收器");
			//从Intent对象当中，取出歌词信息，然后更新TestView
			String lrcMessageString=intent.getStringExtra("lrcMessage");
			lrcText.setText(lrcMessageString);
		}
	}
	
	
	
}
